<?php
class HomeCommonAction extends Action {
	public function _initialize(){
		if(in_array(MODULE_NAME,array("Follow"))){
			$this->checkLogin();
		} 
	}
	
    public function checkLogin(){
        if(!$this->checkMember()){
            showMsg("请先登录");
            return false;
        }
    }

    public function checkMember($type = 1) {    	
        if (Session::getLocal('user') !== 1 && $type == 1) {           
            return false;
        }
        return true;
    }

    public function getMember(){
        if($this->checkMember()){
            return $_SESSION[Session::localName()];
        }
        return false;
    }

    public function logOut() {
        Session::clearLocal(); 
    }

	/**
	 * 获取单条信息
	 * @param unknown_type $model 模型
	 * @param unknown_type $id id
	 * @param unknown_type $map 条件
	 */
	public function getInfo($model,$id,$map){
		if(!$map){
			if($id){
				$map["id"] = $id;
			}elseif($_REQUEST ["id"]){
				$map["id"] = $_REQUEST ["id"];
			}	
		}
		if($map){
			$info = $model->where($map)->find();
		}
//		dump($model->getlastsql());exit;
		return $info;
	}
	/**
	 * 获取少量数据的数据列表
	 * @param $model 模型
	 * @param $map 条件
	 */
	public function getList($model, $map,$order='',$limit='',$group=''){
    	$sort = $model->getPk ();
		$order = $order ? $order : "`".$sort."` desc";
		$limit = $limit ? $limit :"";
		$select = $model->where($map)->order($order)->limit($limit)->Group($group)->select();
		return $select;
	}
	/**
	 * 获取少量数据的数据列表
	 * @param $model 模型
	 * @param $map 条件
	 */
	public function getListField($model, $map,$field,$order='',$limit='',$group=''){
    	$sort = $model->getPk ();
		$order = $order ? $order : "`".$sort."` desc";
		$limit = $limit ? $limit :"";
		$select = $model->where($map)->order($order)->limit($limit)->Group($group)->field($field)->select();
		return $select;
	}
	
    public function getCount($model, $map,$order='',$limit='',$group=''){
		$count = $model->where($map)->order($order)->limit($limit)->Group($group)->count();
    	return $count;
    }
    
    public function _list($model,$map,$order='',$limit,$mapx){
    	$sort = $model->getPk ();
    	$order = $order ? $order : "`".$sort."` desc";
    	
    	$count = $model->where ( $map )->count ( 'id' );
//    	echo $model->getlastsql();
    	if ($count > 0) {
			import ( "ORG.Util.Page" );
			$listRows = $limit ? $limit : '10';
			$p = new Page ( $count, $listRows );
			$voList = $model->where($map)->order($order)->limit($p->firstRow . ',' . $p->listRows)->select();
			//分页时保证跳转结构
			foreach ( $mapx as $key => $val ) {
				if (! is_array ( $val ) && $key != '_string') {
					$p->parameter .= "$key=" . urlencode ( $val ) . "&";
				}
			}
			if($_REQUEST['pageArr']) {
				foreach ( $_REQUEST['pageArr'] as $key => $val ) {
					if (! is_array ( $val )) {
						$p->parameter .= "$key=" . urlencode ( $val ) . "&";
					}
				}
			}
			$page = $p->show ();
			$this->assign ( 'volist', $voList );
			$this->assign ( 'order', $order );
			$this->assign ( 'page', $page );
			$this->assign ( 'count',$count);
			$this->assign ( 'totalPages',$p->totalPages);
			$this->assign ( 'nowPage',$p->nowPage);
//			dump($voList);
			return $voList;
    	}
    }
    
    /**
     * 上传图片
     * @param unknown_type $path
     * @param unknown_type $rule
     */
	public function uploads($path,$rule){
		$up = 'Public/Uploads/';
		if(!is_dir($up)){
			mkdir($up);
		}
		$up_path = $up.$path;
		if(!is_dir($up_path)){
			mkdir($up_path);
		}
		import("ORG.Net.UploadFile");
		$upload = new UploadFile(); // 实例化上传类
		$upload->maxSize  = 3145728 ; // 设置附件上传大小
		$upload->minSize  = 0 ; // 设置附件上传大小
		$upload->allowExts  = $rule['allowExts'] ? $rule['allowExts'] : array('jpeg','jpg','gif','png','bmp','txt','doc','docx','xls','ppt','pdf'); // 设置附件上传类型
		$upload->savePath = $up_path; // 设置附件上传目录
		$upload->saveRule = 'uniqid';

		//生成缩略图
		$upload->thumb = true;
		$upload->thumbMaxWidth = 1000;
		$upload->thumbMaxHeight = 100;

		if(!$upload->upload()) { // 上传错误 提示错误信息
			showMsg($upload->getErrorMsg());
		}else{ // 上传成功 获取上传文件信息
			$info =  $upload->getUploadFileInfo();
		}
		return $info;
	}
	
    
    
    
    
    
    
    
    

}
?>